$(document).ready(function(){
    getshopInfo();

    var provinceTag = document.getElementById("province");
    
    for(var i=0; i<provinceList.length; i++){
        var province = provinceList[i];
        provinceTag.add(new Option(province.name,i));
    }
    
    $('#province').on('change',function(){
        var province = $(this).find('option:selected').text()
        if(province == '北京'||province == '上海'||province == '天津'||province  == '重庆'){
            $('.cityTxt').css('display','none')
            $('#city').css('display','none')
        }else if(province == '请选择'){
            $('.cityTxt').css('display','none')
            $('#city').css('display','none')
        }else{
            $('.cityTxt').css('display','block')
            $('#city').css('display','block')
            $("#city").empty();
            $('#city').append('<option value="-1">请选择</option>')
            for(var i=0; i<provinceList.length; i++){
                if(provinceList[i].name == province){
                    // console.log()
                    if(provinceList[i].cityList){
                        for(var j in provinceList[i].cityList){
                            $('#city').append('<option value="'+j+'">'+provinceList[i].cityList[j].name+'</option>')
                        }
                    }
                    
                }
            }
        }
    })

    //获取地址
    $('#shopAddress').on('input propertychange',function(){
        var keyword = $(this).val();
        // console.log(keyword);
        var region = '天津';
        if(keyword != ''){
            getAjax(`${global.apiUrl}/Api/Url/ceshi`,res =>{
                // console.log(res);
                if(res.status == 0){
                    var html = '';
                    for(var i in res.data){
                        html += `
                        <div class="address-list-item themeColor">
                            <div class="address-title">${res.data[i].title}</div>
                            <div class="address-address">${res.data[i].address}</div>
                            <input type="hidden" class="lat" value="${res.data[i].location.lat}">
                            <input type="hidden" class="lng" value="${res.data[i].location.lng}">
                        </div>
                    `
                    }
                    $('.address-list').html(html);
                }
            },{
                keyword:keyword,
                key:'6VNBZ-2YSC3-UAK3W-YDOS4-QLWCT-OWBXM',
                region:region
            })
        }else{
            $('.address-list').html('');
        }

    });
    $('.address-list').on('click','.address-list-item',function(){
        var address = $(this).find('.address-address').text();
        var title = $(this).find('.address-title').text();
        var lat = $(this).find('.lat').val();
        var lng = $(this).find('.lng').val();
        $('.address-list').html('');
        $('#shopAddress').val(`${title} (${address})`)
        $('#shop-address-lat').val(lat)
        $('#shop-address-lng').val(lng)
    })
    function setShopImgUrl(res){
        document.getElementById('shop-img-url').value = res;
    }
    imgUpload('shop-img-select','shop-img-show',setShopImgUrl);

});

/*** 商户信息 ***/
function getshopInfo(){
    getAjax(`${global.apiUrl}/admin_get_community_health_shop_content`,function(res){
        console.log(res);
        if(!res) return
        shopInfo = res
        $('#shopName').val(shopInfo.shop_name);
        $("#shopPhone").val(shopInfo.shop_phone);
        $("#shopAddress").val(shopInfo.address);
        $('#shop-img-url').val(shopInfo.shop_img);
        if(shopInfo.shop_img){

            $('#shop-img-show').html(`<img src="${shopInfo.shop_img}" />`);
        }
        //商家描述
        $('#shopDesc').val(shopInfo.content);
        if(shopInfo.sheng){ 
            for(var i=0; i<provinceList.length; i++){
                if(provinceList[i].name == shopInfo.sheng){
                    var province = document.getElementById("province");
                    province.options[i+1].selected=true
                    if(shopInfo.shi && shopInfo.shi != '天津' &&shopInfo.shi != '北京' && shopInfo.shi != '上海' && shopInfo.shi != '重庆'){
                        var city = document.getElementById("city");
                        if(provinceList[i].cityList){
                            for(var j in provinceList[i].cityList){
                                if(provinceList[i].cityList[j].name == shopInfo.shi){
                                    var city = document.getElementById("city");
                                    $('.cityTxt').css('display','block')
                                    $('#city').css('display','block')
                                    city.options[j+1].selected=true
                                }
                            }
                        }
                    }
                }
            }
        }else{
            $("#province").val('-1');
        }
        // if(shopInfo.shi){
        //     $('.cityTxt').css('display','block')
        //     $('#city').css('display','block')
        //     $('#city').val(shopInfo.shi);
        // }else{
        //     $('.cityTxt').css('display','none')
        //     $('#city').css('display','none')
        // }
    },{
        id: global.shop_id//shopId
    })
}


/** 添加 修改 **/
$('#shopInfoBtn').on('click',function(){
    //获取名称
    var shopName = $('#shopName').val();
    //地址及其lat  lng
    var address = $('#shopAddress').val();
    // var lat = $('#shop-address-lat').val();
    // var lng = $('#shop-address-lng').val();
    //商家电话
    var phone = $('#shopPhone').val();
    // console.log('phone' + phone)
    //获取缩略图
    var shopImg = $('#shop-img-url').val();
    //商家描述
    var shopDesc = $('#shopDesc').val();
    //数据
    if(shopName == ''||phone == ''||address==''||shopImg==''||shopDesc==""){
        alert('请将信息填写完整！')
    }else{
        var province = $('#province').find('option:selected').text()
        if(province == '请选择'){
            alert('请将信息填写完整！')
            return;
        }
        var shi = ''
        if(province == '北京'||province == '上海'||province == '天津'||province  == '重庆'){
            shi = province
        }else{
            shi = $('#city').find('option:selected').text();
        }
        var data = {
            "id": shopInfo.id,
            "shop_name":shopName,
            "shop_phone":phone,
            "address":address,
            "shop_img":shopImg,
            "content":shopDesc,
            "sheng": province,
            "shi": shi,
            "community_id":global.community_id,
        }
     }
    data = JSON.stringify(data);
    $.ajax({
        url:`${global.apiUrl}/admin_save_community_health_shop`,
        type:'POST',
        data:{
            pro:data,
        },
        success(res){
            if(res == 1){
                alert('商铺信息，修改成功！');
            }
        },
        error(res){
            // console.log(res);
        }
    })

})